package Backend

import (
	"database/sql"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/jinzhu/gorm"
	"hello/controllers/Base"
	"hello/wl"
)

type XinController struct {
	Base.BaseController
}

func (this *XinController)Test()  {
	fmt.Println("儿子重写")
}
// @router /xin [get]
func (this *XinController) Index() {
	this.Test()


	this.AjaxSuccess(123)
}



// @router /xin2 [get]
func (this *XinController) Select() {


	//username:=beego.AppConfig.Strings("*")
	username:=beego.AppConfig.Strings("dev::mydriver")


	this.Data["json"] =username

	this.ServeJSON()

}

type UserGroup struct {
	gorm.Model
	Id  string `gorm:"primary_key"`
	Name string
}
func (UserGroup) TableName() string {
	return "usergroup"
}

type User struct {
	gorm.Model
	Key  uint `gorm:"primary_key"`
	Username string
	Group string
	UserGroup   UserGroup `gorm:"ForeignKey:Id;AssociationForeignKey:Group"`
}
func (User) TableName() string {
	return "userinfo"
}
// @router /gorm1 [get]
func (this * XinController) Gorm1(){
	var user User
	wl.DB.First(&user)

	var u []User
	wl.DB.Preload("UserGroup").Find(&u)

	fmt.Println(u)

	this.Data["json"]=u
	this.ServeJSON()
}

// @router /gorm2 [get]
func (this * XinController) Gorm2(){
	var u User
	wl.DB.Find(&u)
	fmt.Println(u)
	wl.DB.Model(&u).Association("UserGroup").Find(&u.UserGroup)
	this.Data["json"]=u
	this.ServeJSON()
}

type YIIUser struct {
	Id   uint `gorm:"primary_key"`
	Username    string
	Email 	string
	Created_at  string
	Updated_at string
	YIIPost   []YIIPost `gorm:"ForeignKey:User_id;AssociationForeignKey:Id"`
}

type YIIPost struct {
	Id   uint `gorm:"primary_key"`
	Title    string
	User_id int
	Created_at  string
	Updated_at string
}

func (YIIPost)TableName() string {
	return "post"
}

func (YIIUser)TableName() string {
	return "user"
}


// @router /gorm3 [get]
func (this * XinController) Gorm3(){
	var u []YIIUser

	wl.DB.Preload("YIIPost").Limit(10).Offset(14).Find(&u)
	this.Data["json"]=u
	this.ServeJSON()
}




// @router /gorm [get]
func (this * XinController) Gorm(){
	type UserInfo struct {
		Id  string
		Name string
		Username string
	}

	var users []UserGroup


	wl.DB.Where("id = ?", "gen").First(&users)

	wl.DB.Where("name LIKE ?", "%院%").Find(&users)

	wl.DB.Where(map[string]interface{}{"pid": "gen", "id": "other"}).Find(&users)

	rows, _ := wl.DB.Table("userinfo").Select("userinfo.`username`, userinfo.key,usergroup.`name`").
		Joins("LEFT JOIN usergroup  ON userinfo.`group`=usergroup.`id`").Rows()


	//l := list.New()
	var qie =[]interface{}{}
	if columns, err := rows.Columns(); err != nil {



	} else {

		//拼接记录Map

		values := make([]sql.RawBytes, len(columns))

		scans := make([]interface{}, len(columns))



		for i := range values {

			scans[i] = &values[i]

		}

		//此处遍历在3W记录的时候，长达1分钟甚至更多

		for rows.Next() {

			_ = rows.Scan(scans...)

			each := map[string]interface{}{}

			for i, col := range values {

				each[columns[i]] = string(col)

			}

			qie = append(qie, each)

		}

	}

	//fmt.Println(l)




	//fmt.Println(err,rows)
	//wl.DB.Find(&users)
	//fmt.Println(users)
	this.Data["json"] =qie

	this.ServeJSON()



}

